import requests
import re


def getMaoYinfilm():
    """获取猫眼电影的top100"""

    url = "https://maoyan.com/board/4"  # 排行榜网址
    totalList = list()

    for i in range(0, 10):
        data = {
            'offset': i*10
        }
        res = requests.get(url, params=data)

        html = res.content.decode('utf-8')

        pat = r'<dd>\s*<i class="board-index.*?>(\d*)</i>[\s\S]*?<img data-src="(.*?)"[\s\S]*?>[\s\S]*?<p class="name">.*?title="(.*?)".*?</p>\s*<p class="star">\s*(.*?)\s*</p>\s*<p class="releasetime">(.*?)</p>[\s\S]*?<p class="score"><i class="integer">(.*?)</i><i class="fraction">(.*?)</i>[\s\S]*?</dd>'
        perlist = re.findall(pat, html)
        totalList.extend(perlist)

    return totalList


def saveAsFile():
    file = open("filmResult.txt", mode='w+', encoding="utf8")
    filmList = getMaoYinfilm()
    for film in filmList:
        print(film)
        file.write("序号："+film[0]+"，图片："+film[1] +
                   "，电影名称："+film[2]+"，主演："+film[3]+"，时间："+film[4]+"，评分："+film[5]+film[6])
        file.write("\n")
    file.close()


if __name__ == '__main__':
    saveAsFile()
